package com.net.xpay.admin.controller.admin.rbac;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.net.common.model.ResultData;
import com.net.common.util.StreamUtil;
import com.net.xpay.admin.secutiry.RequestUser;
import com.net.xpay.common.domain.admin.rbac.AdminUser;
import com.net.xpay.common.domain.admin.rbac.Menu;
import com.net.xpay.common.manager.admin.rbac.MenuManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 2019/6/4 16:29

 */
@RestController
@RequestMapping("/admin/info")
@Api(tags = "运营/管理员用户信息查询", description = "运营 管理员用户信息查询 net")
public class AdminInfoController {


    @Autowired
    private MenuManager menuManager;

    @ApiOperation("获取当前用户信息")
    @GetMapping
    @ResponseBody
    public ResultData<AdminUser> getInfo(@RequestUser AdminUser adminUser) {
        //按钮ops 控制页面显示按钮
        List<Menu> menus = menuManager.findByAdminUserId(adminUser.getId());
        menus = StreamUtil.distinct(menus, Menu::getId);
        adminUser.setRoles(Lists.newArrayList(menus.stream().filter(menu -> !Strings.isNullOrEmpty(menu.getOp())).map(Menu::getOp).collect(Collectors.toSet())));
        if (Boolean.TRUE.equals(adminUser.getSuperAdmin())) {
            adminUser.getRoles().add("ADMIN");
        }
        adminUser.setPassword(null);
        return new ResultData<>(adminUser);
    }
}
